package com.csun.cmny.provider.consumer;

import com.csun.cmny.base.constant.AliyunMqTopicConstants;
import com.csun.cmny.base.constant.WebSocketConstant;
import com.csun.cmny.core.mq.MqMessage;
import com.csun.cmny.provider.model.dto.SendWebSocketRequest;
import com.csun.cmny.provider.web.websocket.WebSocketServer;
import com.csun.cmny.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-10-10 10:41
 * @Description: the class opc send web socket topic consumer
 */
@Slf4j
@Service
public class OpcSendWebSocketTopicConsumer {

    @Resource
    private WebSocketServer webSocketServer;

    /**
     * Handler send websocket topic.
     *
     * @param body      the body
     * @param topicName the topic name
     * @param tags      the tags
     * @param keys      the keys
     */
    public void handlerSendWebSocketTopic(String body, String topicName, String tags, String keys) {
        MqMessage.checkMessage(body, keys, topicName);
        // SendSmsRequest sendSmsRequest;

        SendWebSocketRequest sendWebSocketRequest;

        try {
            sendWebSocketRequest = JacksonUtil.parseJson(body, SendWebSocketRequest.class);

        } catch (Exception e) {
            log.error("SendWebSocketRequest <== JSON 转换异常={}", e.getMessage(), e);
            throw new IllegalArgumentException("SendWebSocketRequest <== JSON 转换异常", e);
        }

        String msgType = sendWebSocketRequest.getMsgType();
        if (StringUtils.isEmpty(msgType)) {
            throw new IllegalArgumentException("msgType不能为空");
        }

        try {
            switch (msgType) {
                case WebSocketConstant.WEBSOCKET_WITH_USER_ID_LIST :
                    webSocketServer
                            .sendMessageWithUserId(sendWebSocketRequest.getUserIdList(),
                                    sendWebSocketRequest.getMessage());
                    break;
                case WebSocketConstant.WEBSOCKET_WITH_GROUP_ID :
                    webSocketServer
                            .sendMessageWithGroupId(sendWebSocketRequest.getGroupId(),
                                    sendWebSocketRequest.getMessage());
                    break;
                case WebSocketConstant
                        .WEBSOCKET_WITH_ROLE_CODE_LIST :
                    webSocketServer
                            .sendMessageWithRoleCode(sendWebSocketRequest.getRoleCodeList(),
                                    sendWebSocketRequest.getGroupId(),sendWebSocketRequest.getMessage());
                    break;
                case WebSocketConstant.WEBSOCKET_DEFAULT:
                    webSocketServer.sendMessageDefault(sendWebSocketRequest);
                    break;
            }
        } catch (Exception e) {
            log.error("WebSocket发送异常={}", e.getMessage(), e);
            throw new IllegalArgumentException("WebSocket发送异常", e);
        }
    }

}
